package com.AEWMS.Service;

import com.AEWMS.Dao.Admin;
import com.AEWMS.Dao.AttendanceType;
import com.AEWMS.Dao.Class;
import com.AEWMS.Dao.College;
import com.AEWMS.Utlis.DbCon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author lhs
 * @create 2021/2/22 16:16
 */
public class AttendanceTypeService {

    private Connection conn = null ;
    private PreparedStatement pstmt = null ;

    public AttendanceTypeService() {
        DbCon dbcon;
        try {
            dbcon = new DbCon();
            this.conn = dbcon.getConnection();
        } catch (Exception e) {
            System.out.println("AttendanceTypeService 连接数据库失败");
            e.printStackTrace();
        }
    }

    public void closeCon(){
        try {
            this.conn.close();
        } catch (Exception e) {
            System.out.println("数据库连接关闭");
            e.printStackTrace();
        }
    }

    /**
     * 查找所有考勤类型
     * @return 所有考勤类型信息
     */
    public ArrayList<AttendanceType> findAllAttendType(){
        ArrayList<AttendanceType> AllAttendTypeList = new ArrayList();//升级版数组，返回所有考勤类型对象
        String sql = "SELECT * FROM attendance_type_info";
        try {
            this.pstmt = conn.prepareStatement(sql);
            ResultSet rs = this.pstmt.executeQuery();
            while (rs.next()){
                AttendanceType s = new AttendanceType();//按行查找，为false时查询结束
                s.setAttendanceTypeId(rs.getInt(1));
                s.setAttendanceTypeName(rs.getString(2));
                s.setAttendanceStandard(rs.getFloat(3));
                AllAttendTypeList.add(s);//将查找到的行（每个考勤类型对象）添加到数组
            }
        } catch (Exception e) {
            System.out.println("findAllAttendType 失败");
            System.out.println(e);
        }
        return AllAttendTypeList;//输出包含所有考勤类型对象的数组
    }

    /**
     * 查找考勤类型
     * @param id 通过id
     * @return 返回一个考勤类型对象
     */
    public AttendanceType findAttendTypeById(String id){
        AttendanceType s = new AttendanceType();
        String sql = "SELECT * FROM attendance_type_info WHERE attendance_type_id = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setAttendanceTypeId(rs.getInt(1));
                s.setAttendanceTypeName(rs.getString(2));
                s.setAttendanceStandard(rs.getFloat(3));
            }
        }
        catch (Exception e) {
            System.out.println("findAttendTypeById 失败");
            System.out.println(e);
        }
        return s;
    }

    /**
     * 添加考勤类型
     * @return 一个考勤类型对象
     */
    public boolean insertAttendType(AttendanceType s){
        boolean flag = false;
        String sql = "INSERT INTO attendance_type_info VALUES(?,?,?)";
        try {
            this.pstmt = conn.prepareStatement(sql);//sql语句已经在这个准备对象里，有问号就要用准备对象
            this.pstmt.setInt(1,s.getAttendanceTypeId());
            this.pstmt.setString(2,s.getAttendanceTypeName());
            this.pstmt.setFloat(3,s.getAttendanceStandard());
            int rs = this.pstmt.executeUpdate();//这里不用再传sql
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("insertAttendType defeat");
            System.out.println(e);
        }
        return flag;
    }

    /**
     * 修改一个考勤类型
     * @param s 一个考勤类型对象
     * @return 是否成功
     */
    public boolean updateAttendType(AttendanceType s) {
        boolean flag = false;
        String sql = "UPDATE attendance_type_info SET attendance_type_name = ?,attendance_standard = ? WHERE attendance_type_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,s.getAttendanceTypeName());
            this.pstmt.setFloat(2,s.getAttendanceStandard());
            this.pstmt.setInt(3,s.getAttendanceTypeId());
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("updateAttendType 失败");
            System.out.println(e);
        }
        return flag;

    }

    /**
     * 删除一个考勤类型
     * @param id 考勤类型id
     * @return 是否成功
     */
    public boolean deleteAttendTypeById(String id){
        boolean flag = false;
        String sql = "DELETE FROM attendance_type_info WHERE attendance_type_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,id);//这里的名字与dao.admin传进来的一致
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("deleteAttendTypeById 失败");
            System.out.println(e);
        }
        return flag;
    }

}




